home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / bin / DXUtils / AppWizard / DXAppwiz.awx / TEMPLATE / DSUTIL.H < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-25  |  5.7 KB  |  170 lines

  1. //-----------------------------------------------------------------------------
  2. // File: DSUtil.h
  3. //
  4. // Desc: 
  5. //-----------------------------------------------------------------------------
  6. #ifndef DSUTIL_H
  7. #define DSUTIL_H
  8.  
  9. #include <windows.h>
  10. #include <mmsystem.h>
  11. #include <mmreg.h>
  12. #include <dsound.h>
  13.  
  14.  
  15.  
  16.  
  17. //-----------------------------------------------------------------------------
  18. // Classes used by this header
  19. //-----------------------------------------------------------------------------
  20. class CSoundManager;
  21. class CSound;
  22. class CStreamingSound;
  23. class CWaveFile;
  24.  
  25.  
  26.  
  27.  
  28. //-----------------------------------------------------------------------------
  29. // Typing macros 
  30. //-----------------------------------------------------------------------------
  31. #define WAVEFILE_READ   1
  32. #define WAVEFILE_WRITE  2
  33.  
  34. #define DSUtil_StopSound(s)         { if(s) s->Stop(); }
  35. #define DSUtil_PlaySound(s)         { if(s) s->Play( 0, 0 ); }
  36. #define DSUtil_PlaySoundLooping(s)  { if(s) s->Play( 0, DSBPLAY_LOOPING ); }
  37.  
  38.  
  39.  
  40.  
  41. //-----------------------------------------------------------------------------
  42. // Name: class CSoundManager
  43. // Desc: 
  44. //-----------------------------------------------------------------------------
  45. class CSoundManager
  46. {
  47. protected:
  48.     LPDIRECTSOUND8 m_pDS;
  49.  
  50. public:
  51.     CSoundManager();
  52.     ~CSoundManager();
  53.  
  54.     HRESULT Initialize( HWND hWnd, DWORD dwCoopLevel, DWORD dwPrimaryChannels, DWORD dwPrimaryFreq, DWORD dwPrimaryBitRate );
  55.     inline  LPDIRECTSOUND8 GetDirectSound() { return m_pDS; }
  56.     HRESULT SetPrimaryBufferFormat( DWORD dwPrimaryChannels, DWORD dwPrimaryFreq, DWORD dwPrimaryBitRate );
  57.     HRESULT Get3DListenerInterface( LPDIRECTSOUND3DLISTENER* ppDSListener );
  58.  
  59.     HRESULT Create( CSound** ppSound, LPTSTR strWaveFileName, DWORD dwCreationFlags = 0, GUID guid3DAlgorithm = GUID_NULL, DWORD dwNumBuffers = 1 );
  60.     HRESULT CreateFromMemory( CSound** ppSound, BYTE* pbData, ULONG ulDataSize, LPWAVEFORMATEX pwfx, DWORD dwCreationFlags = 0, GUID guid3DAlgorithm = GUID_NULL, DWORD dwNumBuffers = 1 );
  61.     HRESULT CreateStreaming( CStreamingSound** ppStreamingSound, LPTSTR strWaveFileName, DWORD dwCreationFlags, GUID guid3DAlgorithm, DWORD dwNotifyCount, DWORD dwNotifySize, HANDLE hNotifyEvent );
  62. };
  63.  
  64.  
  65.  
  66.  
  67. //-----------------------------------------------------------------------------
  68. // Name: class CSound
  69. // Desc: Encapsulates functionality of a DirectSound buffer.
  70. //-----------------------------------------------------------------------------
  71. class CSound
  72. {
  73. protected:
  74.     LPDIRECTSOUNDBUFFER* m_apDSBuffer;
  75.     DWORD                m_dwDSBufferSize;
  76.     CWaveFile*           m_pWaveFile;
  77.     DWORD                m_dwNumBuffers;
  78.  
  79.     HRESULT RestoreBuffer( LPDIRECTSOUNDBUFFER pDSB, BOOL* pbWasRestored );
  80.  
  81. public:
  82.     CSound( LPDIRECTSOUNDBUFFER* apDSBuffer, DWORD dwDSBufferSize, DWORD dwNumBuffers, CWaveFile* pWaveFile );
  83.     virtual ~CSound();
  84.  
  85.     HRESULT Get3DBufferInterface( DWORD dwIndex, LPDIRECTSOUND3DBUFFER* ppDS3DBuffer );
  86.     HRESULT FillBufferWithSound( LPDIRECTSOUNDBUFFER pDSB, BOOL bRepeatWavIfBufferLarger );
  87.     LPDIRECTSOUNDBUFFER GetFreeBuffer();
  88.     LPDIRECTSOUNDBUFFER GetBuffer( DWORD dwIndex );
  89.  
  90.     HRESULT Play( DWORD dwPriority = 0, DWORD dwFlags = 0 );
  91.     HRESULT Stop();
  92.     HRESULT Reset();
  93.     BOOL    IsSoundPlaying();
  94. };
  95.  
  96.  
  97.  
  98.  
  99. //-----------------------------------------------------------------------------
  100. // Name: class CStreamingSound
  101. // Desc: Encapsulates functionality to play a wave file with DirectSound.  
  102. //       The Create() method loads a chunk of wave file into the buffer, 
  103. //       and as sound plays more is written to the buffer by calling 
  104. //       HandleWaveStreamNotification() whenever hNotifyEvent is signaled.
  105. //-----------------------------------------------------------------------------
  106. class CStreamingSound : public CSound
  107. {
  108. protected:
  109.     DWORD m_dwLastPlayPos;
  110.     DWORD m_dwPlayProgress;
  111.     DWORD m_dwNotifySize;
  112.     DWORD m_dwNextWriteOffset;
  113.     BOOL  m_bFillNextNotificationWithSilence;
  114.  
  115. public:
  116.     CStreamingSound( LPDIRECTSOUNDBUFFER pDSBuffer, DWORD dwDSBufferSize, CWaveFile* pWaveFile, DWORD dwNotifySize );
  117.     ~CStreamingSound();
  118.  
  119.     HRESULT HandleWaveStreamNotification( BOOL bLoopedPlay );
  120.     HRESULT Reset();
  121. };
  122.  
  123.  
  124.  
  125.  
  126. //-----------------------------------------------------------------------------
  127. // Name: class CWaveFile
  128. // Desc: Encapsulates reading or writing sound data to or from a wave file
  129. //-----------------------------------------------------------------------------
  130. class CWaveFile
  131. {
  132. public:
  133.     WAVEFORMATEX* m_pwfx;        // Pointer to WAVEFORMATEX structure
  134.     HMMIO         m_hmmio;       // MM I/O handle for the WAVE
  135.     MMCKINFO      m_ck;          // Multimedia RIFF chunk
  136.     MMCKINFO      m_ckRiff;      // Use in opening a WAVE file
  137.     DWORD         m_dwSize;      // The size of the wave file
  138.     MMIOINFO      m_mmioinfoOut;
  139.     DWORD         m_dwFlags;
  140.     BOOL          m_bIsReadingFromMemory;
  141.     BYTE*         m_pbData;
  142.     BYTE*         m_pbDataCur;
  143.     ULONG         m_ulDataSize;
  144.     CHAR*         m_pResourceBuffer;
  145.  
  146. protected:
  147.     HRESULT ReadMMIO();
  148.     HRESULT WriteMMIO( WAVEFORMATEX *pwfxDest );
  149.  
  150. public:
  151.     CWaveFile();
  152.     ~CWaveFile();
  153.  
  154.     HRESULT Open( LPTSTR strFileName, WAVEFORMATEX* pwfx, DWORD dwFlags );
  155.     HRESULT OpenFromMemory( BYTE* pbData, ULONG ulDataSize, WAVEFORMATEX* pwfx, DWORD dwFlags );
  156.     HRESULT Close();
  157.  
  158.     HRESULT Read( BYTE* pBuffer, DWORD dwSizeToRead, DWORD* pdwSizeRead );
  159.     HRESULT Write( UINT nSizeToWrite, BYTE* pbData, UINT* pnSizeWrote );
  160.  
  161.     DWORD   GetSize();
  162.     HRESULT ResetFile();
  163.     WAVEFORMATEX* GetFormat() { return m_pwfx; };
  164. };
  165.  
  166.  
  167.  
  168.  
  169. #endif // DSUTIL_H
  170.